<?php

class tbatvController {
	
	// CONTRACT: getCurrentWeek : null -> number
	// PURPOSE 
	// getCurrentWeek gives us a week number (10 through 60, 10-step), or -1 if there is no week associated with the current date.
	function getCurrentWeek() {
		global $week_definitions;
		$date = time();
		//if there are week definitions for the current year
		if (array_key_exists(date('Y'), $week_definitions))
		{
			$current_year = $week_definitions[date('Y')];
			//loop through all the elements of $current_year and check if the date is between the start/end dates of these weeks
			foreach ($current_year as $week => $dates)
			{
				$start = strtotime($dates[0]);
				$end = strtotime($dates[1] . "+1 day"); //otherwise saturday at 12a is considered the end of an event
				if (($date <= $end) && ($date >= $start))
				{
					//the current date is in between the start and end dates of this week
					return $week;
				}
			}
		}
		
		//if all else fails, return invalid week
		return -1;
	}
	
	// CONTRACT: getEventsByWeek : number, number -> List of Events
	// PURPOSE
	// getEventsByWeek outputs an array of Event Objects that are occuring in the specified week-year.
	// If no year is provided, the current year is assumed.
	function getEventsByWeek($week, $year=Null) {
		if ($year == Null) $year = date('Y'); //replace null date
		
		$filter = "SELECT eventid FROM events WHERE week = {$week} AND year = {$year}";
		$query = mysql_query($filter);
		$events = array();
		while ($event = mysql_fetch_array($query))
		{
			$events[] = new Event($event["eventid"]);
		}
		
		return $events;
	}
	
	// CONTRACT : getAllYears : number -> list of numbers
	// PURPOSE
	// getAllYears() takes (optionally) a year, and returns all other years with events in the system
	function getAllYears($year = NULL) {
		$historic_event_query_filter = "SELECT DISTINCT events.year FROM events WHERE events.year != '$year' ORDER BY events.year DESC";
		$historic_event_query = mysql_query($historic_event_query_filter);
		
		//construct list
		$list_of_years = array();
		while ($year = mysql_fetch_array($historic_event_query)) {
			if ($year["year"] > 0) {
				$list_of_years[] = $year["year"];
			}
		}
		
		return $list_of_years;
	}
	
	// CONTRACT : stringWLT : array -> string
	// PURPOSE
	// stringWLT() takes an array with a "win", "lose" and "tie" value and returns it as a formatted string.
	function stringWLT($record) {
		return "(" . $record["win"] . "-" . $record["lose"] . "-" . $record["tie"] . ")";
	}
	
	// CONTRACT: stringWeek : number -> string
	// PURPOSE
	// stringWeek() takes a week number code and turns it into the right noun for the week based on values in constants.php
	function stringWeek($week) {
		global $weeknames;
		return $weeknames[$week];
	}
	
	// CONTRACT: stringComplevel : number -> string
	// PURPOSE
	// stringComplevel() takes a competition level and turns it into the right name for that competition level based on the values in constants.php
	function stringComplevel($complevel) {
		global $complevelnames;
		return $complevelnames[$complevel];
	}
	
	function stringComplevelShort($complevel) {
		global $complevelshort;
		return $complevelshort[$complevel];
	}
	
	// CONTRACT: stringAlliance : array -> string
	// PURPOSE
	// stringAlliance takes an array of teams and turns it into a nicely formatted team string.
	function stringAlliance($alliance) {
		$output = "";
		foreach ($alliance as $key=>$team) {
			$output = $output . "<a href=\"team.php?team=$team\" title=\"Team $team\">$team</a>";
			if (array_key_exists($key + 1, $alliance)) $output = $output . ", ";
		}
		return $output;
	}

	// CONTRACT: buildLocation : string, string, string -> string
	// PURPOSE
	// buildLocation() takes (optionally) a city, state, country and returns a properly formatted location.
	function buildLocation($city = NULL, $state = NULL, $country = NULL) {
		$state = str_replace(",", "", $state);
		$location = "$city";
		if ($state != NULL AND $state != '') $location = $location . ", $state";
		if ($country != NULL AND $country != '') $location = $location . ", $country";
		return $location;
	}
	
	//generates a hyperlink to a team's page, or a questionmark if we don't have a team.
    function linkTeam($teamnumber, $year = Null, $string = Null) {
        global $BASE_PATH;
		global $MOD_REWRITE_ON;
		if ($year == Null) $year = date('Y');
		if ($string == Null) $string = $teamnumber;
    	if ($teamnumber == 0) {
    		return "?";
    	} elseif ($MOD_REWRITE_ON) {
    		   return "<a href=\"" . $BASE_PATH . "team/" . $teamnumber . "/" . $year . "\" title=\"Team " . $teamnumber . "\">" . $string . "</a>";
		} else {
		    return "<a href=\"" . $BASE_PATH . "team.php?team=" . $teamnumber . "&year=" . $year . "\" title=\"Team " . $teamnumber . "\">" . $string . "</a>";
		}
    }
    
    function safeScore($score) {
        if ($score > -1) {
            return $score;
        } else {
            return "?";
        }
    }
}